#include "Boss3Entity.h"

Boss3Entity::Boss3Entity(int id, int type, double x, double y)
{
	_data = new Boss3Data(id, type, x, y);
	_data->rect = new Rect(x, y, 800, 800);
	items = new vector<Boss3ItemEntity*>();

	bossEntity = new BossThreeEntity();

	_door = new Boss3DoorEntity();
	_inActive = false;
	_count = 0;
	initBulletContainer();
}

void Boss3Entity::initSubRect() {
	_data->subRect->push_back(_door->getAbstractData()->getRect());
	_data->subRect->push_back(items->at(0)->getAbstractData()->getRect());
	_data->subRect->push_back(items->at(1)->getAbstractData()->getRect());
	_data->subRect->push_back(bossEntity->getAbstractData()->rect);
}

AbstractEntity* Boss3Entity::getSubEntity(int index) {
	switch (index) {
	case 1: return items->at(0);
	case 2: return items->at(1);
	case 0: return _door;
	default: return bossEntity;
	}
}


/* Set bullet container data */
void Boss3Entity::initBulletContainer() {
	bulletContainer = new BulletContainer(GameDeviceSingleton::getInstance()->getPlayerRect(), BulletArmy);
	bulletContainer->updateData(400, 0, 1, _ID_BULLET_BOSS_3);
}

void Boss3Entity::updateBulletContrainer() {
	if (bossEntity->getAbstractData()->isDead) return;
	if (bulletContainer->checkCanFire()) {
		bulletContainer->fire(_POSITION_X, _POSITION_Y, _BULLET_DIRECTION_DOWN, NULL);
	}
	bulletContainer->update(0);
}

void Boss3Entity::update(long time){
	if(GameDeviceSingleton::getInstance()->getPlayerRect()->X >= _POSITION_X && _inActive == false)
	{
		if(_count == APPEAR_DELAY_TIME)
		{
			_data->canInteract = true;
			bossEntity->getAbstractData()->canInteract = true;
			_inActive = true;
			_count = 0;
		}
		else
		{
			_count++;
		}
	}
	if(!_inActive)
	{
		_data->canInteract = false;
		bossEntity->getAbstractData()->canInteract = false;
		return;
	}

	if(_data->sprites->currentSprite->endOfSheet() && _count == 0)
	{
		_count = 1;
		_data->sprites->currentSprite->setStartEnd(8,10);
		if(items->size() == 0)
		{
			createItems();
		}
	}
	vector<Boss3ItemEntity*>::iterator it = items->begin();
	for(; it != items->end(); it++)
	{
		(*it)->update(time);
	}
	_door->update(time);
	if (bossEntity->getAbstractData()->isDead) {
		_door->getAbstractData()->canInteract = true;
		items->at(0)->updateData(ACTIONS_IS_HURT, 100);
		items->at(1)->updateData(ACTIONS_IS_HURT, 100);
	} else {
		_data->sprites->updateCurrent();
		updateBulletContrainer();
	}
}

void Boss3Entity::draw(long time){
	if(!_inActive)
	{
		return;
	}
	_door->draw(time);
	if (!bossEntity->getAbstractData()->isDead)
		_data->sprites->draw();
	bulletContainer->draw(0);
	vector<Boss3ItemEntity*>::iterator it = items->begin();
	for(; it != items->end(); it++)
	{
		(*it)->draw(time);
	}
}

AbstractData* Boss3Entity::getAbstractData()
{
	return _data;
}

void Boss3Entity::updateData(int actions, int actiondata)
{
	if(actiondata > 0)
	{
		if(actions == ACTIONS_BOSS3_ITEM_IN_LAND)
		{
			items->at(actiondata-1)->updateData(actions, 0);
		}
	}

	if (actions == ACTIONS_IS_HURT) {
		int index = actiondata / 10;
		int dam = actiondata % 10;
		getSubEntity(index)->updateData(actions, dam);
	}
}


void Boss3Entity::createItems()
{
	Boss3ItemEntity* leftItem = new Boss3ItemEntity(ItemType::ITEM_LEFT);
	Boss3ItemEntity* rightItem = new Boss3ItemEntity(ItemType::ITEM_RIGHT);

	items->push_back(leftItem);
	items->push_back(rightItem);
	initSubRect();
}